"""
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""

import cv2


# 1、创建VideoCapture的对象cap，并对视频的width*high设置
cap1 = cv2.VideoCapture(0 + cv2.CAP_DSHOW)
cap2 = cv2.VideoCapture(1 + cv2.CAP_DSHOW)
cap3 = cv2.VideoCapture(2 + cv2.CAP_DSHOW)

cap1.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap1.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

cap2.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap2.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

cap3.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap3.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)


# 5、确保cap打开了
while True:
    if not cap1.isOpened():
        print("cap1 is not opened, open the cap1")
        cap1.open()
    elif not cap2.isOpened():
        print("cap2 is not opened, open2 the cap")
        cap2.open()
    elif not cap3.isOpened():
        print("cap3 is not opened, open3 the cap")
        cap2.open()
    else:
        print('cap1/2/3 is opened, read the video stream...')
        break


# 6、使用一个While循环不间断地对usb摄像头进行读取，一直到遇到键盘终止事件时break掉
while cap1.isOpened():
    # 使用cap.read()从摄像头读取一帧
    ret1, frame1 = cap1.read()
    ret2, frame2 = cap2.read()
    ret3, frame3 = cap3.read()

    cv2.imshow('frame1', frame1)
    cv2.imshow('frame2', frame2)
    cv2.imshow('frame3', frame3)

    if cv2.waitKey(1) == ord('q'):
        break

cap1.release()
cap2.release()
cap3.release()
print('release cap')
cv2.destroyAllWindows()
print('release all windows')
